package com.ruoyi.server.api.memberCoach.service.impl;

import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.server.api.hourRecord.domain.HourRecord;
import com.ruoyi.server.api.hourRecord.mapper.HourRecordMapper;
import com.ruoyi.server.api.memberCoach.domain.MemberCoach;
import com.ruoyi.server.api.memberCoach.mapper.MemberCoachMapper;
import com.ruoyi.server.api.memberCoach.model.MemberCoachAndCoachManagement;
import com.ruoyi.server.api.memberCoach.service.IMemberCoachService;
import com.ruoyi.server.api.memberUserManagement.mapper.MemberUserManagementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 会员用户教练关系Service业务层处理
 *
 * @author zmh
 * @date 2024-06-12
 */
@Service
public class MemberCoachServiceImpl implements IMemberCoachService
{
    @Autowired
    private MemberCoachMapper memberCoachMapper;
    @Autowired
    private HourRecordMapper hourRecordMapper;
    @Autowired
    private MemberUserManagementMapper memberUserManagementMapper;

    /**
     * 查询会员用户教练关系列表
     *
     * @param id venueUniqueIdentifier 会员用户教练关系
     * @return 会员用户教练关系
     */
    @Override
    public List<MemberCoachAndCoachManagement> memberCoachById(Integer id, String venueUniqueIdentifier) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
         venueUniqueIdentifier = loginUser.getUser().getVenueUniqueIdentifier();
        return memberCoachMapper.memberCoachById(id,venueUniqueIdentifier);
    }
    /**
    * @Author ZMH
    * @Description 续课
    * @Date 2024/6/13 18:59
    * @Return
    * @param courseTime,courseId,venueUniqueIdentifier
    */
    @Override
    public int addCourseTime(Integer courseTime , Integer courseId,Integer memberUserId  , String venueUniqueIdentifier) {
        //获取当前登录用户
        LoginUser loginUser = SecurityUtils.getLoginUser();
        //设置当前登录用户的场馆唯一标识
        venueUniqueIdentifier = loginUser.getUser().getVenueUniqueIdentifier();
        MemberCoach memberCoach = memberCoachMapper.selectMemberCoachById(courseId, memberUserId, venueUniqueIdentifier);
        //获取旧课程时长
        Integer oldCourseTime = memberCoach.getCourseTime();
        //计算新课程时长
        courseTime = courseTime + oldCourseTime;
        int i = memberCoachMapper.addCourseTime(courseTime, courseId, memberUserId, venueUniqueIdentifier);
        if ( i > 0){
            HourRecord hourRecord = new HourRecord();
            hourRecord.setVenueUniqueIdentifier(venueUniqueIdentifier);
            hourRecord.setMemberUserId(memberUserId);
            hourRecord.setCoachId(courseId);
            hourRecord.setOperator(loginUser.getUsername());
            hourRecord.setStatus(0);
            //获取添加的课时时长
            int addCourseTime=courseTime-oldCourseTime;
            hourRecord.setStatusHour(addCourseTime);
            hourRecord.setCreateTime(new Date());
            hourRecord.setUpdateTime(new Date());
            hourRecordMapper.insertHourRecord(hourRecord);
            return 1;
        }else {
            return 0;
        }
    }
    /**
     * @Author ZMH
     * @Description 退课程时长
     * @Date 2024/6/13 18:59
     * @Return
     * @param courseTime,memberUserId,courseId,venueUniqueIdentifier
     */
//    @Override
//    public int refundCourseTime(Integer courseTime, Integer courseId, Integer memberUserId, String venueUniqueIdentifier) {
//        //获取当前登录用户
//        LoginUser loginUser = SecurityUtils.getLoginUser();
//        //设置当前登录用户的场馆唯一标识
//        venueUniqueIdentifier = loginUser.getUser().getVenueUniqueIdentifier();
//        MemberCoach memberCoach = memberCoachMapper.selectMemberCoachById(courseId, memberUserId, venueUniqueIdentifier);
//        //获取旧课程时长
//        Integer oldCourseTime = memberCoach.getCourseTime();
//        //退课之后的时长
//        int deleteCourseTime = oldCourseTime - courseTime;
//        memberCoach.setCourseTime(deleteCourseTime);
//        int i = memberCoachMapper.refundCourseTime(courseTime, courseId, memberUserId, venueUniqueIdentifier);
//        if ( i > 0){
//            HourRecord hourRecord = new HourRecord();
//            hourRecord.setVenueUniqueIdentifier(venueUniqueIdentifier);
//            hourRecord.setMemberUserId(memberUserId);
//            hourRecord.setCourseId(courseId);
//            hourRecord.setOperator(loginUser.getUsername());
//            hourRecord.setStatus(1);
//            hourRecord.setStatusHour(courseTime);
//            hourRecord.setCreateTime(new Date());
//            hourRecord.setUpdateTime(new Date());
//            hourRecordMapper.insertHourRecord(hourRecord);
//            return 1;
//        }else {
//            return 0;
//        }
//    }
}
